<!-- src/components/UserList.vue -->
<template>
  <div>
    <el-table :data="users" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄" width="100"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

// 用于存储用户数据
const users = ref([])

// 定义获取用户数据的异步方法
const fetchUsers = async () => {
  try {
    // 调用后端接口，注意接口地址需与后端保持一致
    const response = await axios.get('http://localhost:8080/user/list')
    users.value = response.data
  } catch (error) {
    console.error('获取用户数据失败:', error)
  }
}

// 组件加载完毕后立即获取数据
onMounted(() => {
  fetchUsers()
})
</script>

<style scoped>
/* 可以添加一些样式调整 */
</style>
